package com.b511.swpu.cementbackend.util;

import com.b511.swpu.cementbackend.dto.input.BoreholeConditionInputDto;
import com.b511.swpu.cementbackend.entity.SysUser;
import com.b511.swpu.cementbackend.exception.CementException;
import com.b511.swpu.cementbackend.util.enumeration.Code;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.util.StringUtils;

import javax.persistence.criteria.*;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.*;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import javax.persistence.criteria.Predicate;

public class Help {

    private static Pattern linePattern = Pattern.compile("_(\\w)");

    private static Pattern humpPattern = Pattern.compile("[A-Z]");

    public static String createToken() {
        StringBuilder result = new StringBuilder();
        for (int i = 0; i < 3; i++) {
            result.append(UUID.randomUUID().toString()).append("-");
        }
        return result.append(UUID.randomUUID().toString()).toString();
    }

    public static String encryption(String s) {
        try {
            MessageDigest md = MessageDigest.getInstance("md5");
            byte[] bytes = md.digest(s.getBytes());
            return Base64.getEncoder().encodeToString(bytes);
        } catch (NoSuchAlgorithmException e) {
            e.printStackTrace();
        }
        return null;
    }

    public static Predicate createPredicate(BoreholeConditionInputDto boreholeConditionInputDto, From from, CriteriaBuilder cb) {
        if (null == boreholeConditionInputDto) {
            return cb.conjunction();
        }
        List<Predicate> predicates = new ArrayList<>();
        if (StringUtils.hasText(boreholeConditionInputDto.getWellName())) {
            predicates.add(cb.equal(from.get("wellName"), boreholeConditionInputDto.getWellName()));
        }
        if (StringUtils.hasText(boreholeConditionInputDto.getWellCategory())) {
            predicates.add(cb.equal(from.get("wellCategory"), boreholeConditionInputDto.getWellCategory()));
        }
        if (StringUtils.hasText(boreholeConditionInputDto.getWellType())) {
            predicates.add(cb.equal(from.get("wellType"), boreholeConditionInputDto.getWellType()));
        }
        if (StringUtils.hasText(boreholeConditionInputDto.getDrillDate())) {
            String[] date = boreholeConditionInputDto.getDrillDate().split("&");
            predicates.add(cb.between(from.get("beginDrillDate"), Help.stringToDate(date[0]), Help.stringToDate(date[1])));
        }
        return cb.and(predicates.toArray(new Predicate[0]));
    }


    private static Date stringToDate(String date) {
        try {
            SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");
            return format.parse(format.format(format.parse(date).getTime()));
        } catch (ParseException e) {
            throw new CementException(Code.FAIL);
        }
    }

    static String lineToHump(String str) {
        str = str.toLowerCase();
        Matcher matcher = linePattern.matcher(str);
        StringBuffer sb = new StringBuffer();
        while (matcher.find()) {
            matcher.appendReplacement(sb, matcher.group(1).toUpperCase());
        }
        matcher.appendTail(sb);
        return sb.toString();
    }

    static String humpToLine(String str) {
        Matcher matcher = humpPattern.matcher(str);
        StringBuffer sb = new StringBuffer();
        while (matcher.find()) {
            matcher.appendReplacement(sb, "_" + matcher.group(0).toLowerCase());
        }
        matcher.appendTail(sb);
        return sb.toString().substring(1).toUpperCase();
    }
}
